﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sy2_13
{
    public partial class Form1 : Form
    {
        private string specialChar = @"!@#$%^&*()_+-=[]{}|;':"",./<>?";
        public Form1()
        {
            InitializeComponent();
            InitializeStrengthLabels();
        }
        private void InitializeStrengthLabels()
        {
            // 设置4个颜色块的基本属性            
            label1.Text = "";
            label1.Width = 50;
            label1.Height = 20;
            label1.Left = 10;
            label1.Top = 50;
            label2.Text = "";
            label2.Width = 50;
            label2.Height = 20;
            label2.Left = label1.Right + 5;
            label2.Top = 50;
            label3.Text = "";
            label3.Width = 50;
            label3.Height = 20;
            label3.Left = label2.Right + 5;
            label3.Top = 50;
            label4.Text = "";
            label4.Width = 50;
            label4.Height = 20;
            label4.Left = label3.Right + 5;
            label4.Top = 50;
            // 强度描述标签            
            labelStrength.Text = "";
            labelStrength.Left = label4.Right + 10;
            labelStrength.Top = 50;
        }
        // 检查密码强度的函数        
        private int CheckPass(string pass)
        {
            // 检查密码长度            
            if (pass.Length < 8)
                return 0;
            int[] cType = new int[4]; // 0:数字, 1:大写字母, 2:小写字母, 3:特殊字符                        
            foreach (char c in pass)
            {
                if (Char.IsNumber(c))
                {
                    cType[0] = 1;
                }
                else if (Char.IsUpper(c))
                {
                    cType[1] = 1;
                }
                else if (Char.IsLower(c))
                {
                    cType[2] = 1;
                }
                else if (specialChar.IndexOf(c) != -1)
                {
                    cType[3] = 1;
                }
                else
                {
                    // 包含不允许的字符                    
                    return 0;
                }
            }
            // 计算包含的字符类型数量            
            int typeCount = cType[0] + cType[1] + cType[2] + cType[3];
            return typeCount;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string password = textBox1.Text;
            int strength = CheckPass(password);

            // 重置颜色块
            ResetStrengthLabels();

            // 根据强度设置颜色和文本
            switch (strength)
            {
                case 0:
                    MessageBox.Show("密码不得小于8位!\r\n且只能是字母、数字和以下特殊字符的组合:\r\n" + specialChar,
                                  "输入错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;
                case 1:
                    label1.BackColor = System.Drawing.Color.Red;
                    labelStrength.Text = "弱";
                    break;
                case 2:
                    label1.BackColor = System.Drawing.Color.Red;
                    label2.BackColor = System.Drawing.Color.Orange;
                    labelStrength.Text = "较弱";
                    break;
                case 3:
                    label1.BackColor = System.Drawing.Color.Red;
                    label2.BackColor = System.Drawing.Color.Orange;
                    label3.BackColor = System.Drawing.Color.Yellow;
                    labelStrength.Text = "较强";
                    break;
                case 4:
                    label1.BackColor = System.Drawing.Color.Red;
                    label2.BackColor = System.Drawing.Color.Orange;
                    label3.BackColor = System.Drawing.Color.Yellow;
                    label4.BackColor = System.Drawing.Color.Green;
                    labelStrength.Text = "强";
                    break;
            }
        }

        // 重置强度标签颜色
        private void ResetStrengthLabels()
        {
            label1.BackColor = System.Drawing.Color.LightGray;
            label2.BackColor = System.Drawing.Color.LightGray;
            label3.BackColor = System.Drawing.Color.LightGray;
            label4.BackColor = System.Drawing.Color.LightGray;
            labelStrength.Text = "";

        }
    }
}
